
class DexUtils(object):
    def __init__(self, parser_info, user_vars):
        self.parser_info = parser_info
        self.user_vars = user_vars
        self.dexHeader = None
        self.strings = {}
        self.types = {}

    def getLocalScope(self):
        return {} #add the local variables
    
    def _dexHeader(self):
        if self.dexHeader:
            return self.dexHeader
        # read from parser
        dexh = { }
        self.parser_info.parse(0, 112, 'DexHeader', False, 'x', dexh, None)
        self.dexHeader = dexh['x']['DexHeader']
        return self.dexHeader
        
    def getString(self, stringIdx):
        if stringIdx in self.strings:
            return self.strings[stringIdx]
        # try get from parser
        dexh = self._dexHeader()
        ret = {}
        self.parser_info.parse(dexh['stringIdsOff']+stringIdx*4, 4, 'StringId', False, 'x', ret, None)
        stringDataOff = ret['x']['StringId']['stringDataOff']
        self.parser_info.parse(stringDataOff, 0, 'String', False, 'y', ret, None)
        self.strings[stringIdx] = ret['y']['String']
        return self.strings[stringIdx]

    def getTypeString(self, typeIdx):
        if typeIdx in self.types:
            return self.types[typeIdx]

        dexh = self._dexHeader()
        ret = {}
        self.parser_info.parse(dexh['typeIdsOff']+typeIdx*4, 4, 'TypeId', False, 't', ret, None)
        typeDescIdx = ret['t']['TypeId']['descriptorIdx']
        typeString = self.getString(typeDescIdx)
        self.types[typeIdx] = typeString
        return typeString
        


